import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 * 
 * Gui class for shopping cart window
 *
 */
public class CartGui implements ActionListener ,ListSelectionListener {
	/** Gui Class panel */
	public JPanel panel=new JPanel();  //  @jve:decl-index=0:visual-constraint="10,54"
	private static final long serialVersionUID = 1L;
	/** table column names */
	public String[] ColumnNames={"Item","Language","Format","Price"};
	/** table data */
	public String[][] data;
	/** cart label */
	private JLabel cart = null;
	/** buy with credit card button */
	public JButton creditCard = null;
	/** buy with membership button */
	public JButton memberShip = null;
	/** buy a membership button */
	private JButton buyMemberShip = null;
	/** cancel button - go to main screen */
	private JButton cancel = null;
	/** remove book from cart button */
	public JButton remove = null;
	/** membership status label */
	public JLabel memdetails = null;
	/** membership expiration date label */
	public JLabel memexp = null;
	/** scroll panel for the table */
	private JScrollPane jscroll;
	/** table for books */
	public JTable jTable = null;
	/** model for books table */
	public DefaultTableModel model;
	private JButton exit = null;
	
	

	/**
	 * This is the default constructor
	 */
	public CartGui() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() 
	{
		data=Main.cart.g_retrieve();
		cart = new JLabel("Cart");
		cart.setFont(new Font(Font.SANS_SERIF ,Font.ITALIC,40));
		cart.setBounds(new Rectangle(167, 14, 83, 48));
		panel.setSize(new Dimension(533, 375));
		panel.setSize(300, 200);
		panel.setLayout(null);
		panel.add(cart, null);
		panel.add(getJScroll(),null);
		panel.add(getCreditCard(), null);
		panel.add(getCancel(), null);
		panel.add(getRemove(), null);
		panel.add(getMemberShip(), null);
		panel.add(getBuyMemberShip(), null);
		memexp = new JLabel(Main.cart.str[1]);
		memexp.setFont(new Font(Font.SANS_SERIF ,Font.ITALIC,17));
		memexp.setBounds(new Rectangle(214, 242, 142, 33));
		memdetails = new JLabel(Main.cart.str[0]);
		memdetails.setFont(new Font(Font.SANS_SERIF ,Font.ITALIC,17));
		memdetails.setBounds(new Rectangle(18, 241, 181, 38));
		panel.add(memexp,null);
		panel.add(memdetails,null);
		panel.add(getExit(), null);
						
	}
	/**
	 * Initializes the scroll panel for JTable
	 * @return
	 */
	private JScrollPane getJScroll() {
		if (jscroll == null) {
			jscroll = new JScrollPane();
			jscroll.setBounds(new Rectangle(10, 81, 510, 118));
			jscroll.setViewportView(getJTable());
		}
		return jscroll;
	}

	/**
	 * This method initializes creditCard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCreditCard() {
		if (creditCard == null) {
			creditCard = new JButton();
			creditCard.setLayout(new BorderLayout());
			JLabel label1 = new JLabel("Buy With");
			JLabel label2 = new JLabel("Credit Card");
			creditCard.add(BorderLayout.NORTH,label1);
			creditCard.add(BorderLayout.SOUTH,label2);
			creditCard.setBounds(new Rectangle(32, 304, 133, 51));
			creditCard.addActionListener(this);
			if(jTable.getRowCount()==0)
				creditCard.setEnabled(false);
		}
		return creditCard;
	}

	/**
	 * This method initializes memberShip	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getMemberShip() {
		if (memberShip == null) {
			memberShip = new JButton();
			memberShip.setLayout(new BorderLayout());
			JLabel label1 = new JLabel("Buy With");
			JLabel label2 = new JLabel("Membership");
			memberShip.add(BorderLayout.NORTH,label1);
			memberShip.add(BorderLayout.SOUTH,label2);
			memberShip.setBounds(new Rectangle(184, 304, 132, 53));
			memberShip.addActionListener(this);
			if(!Main.cart.str[0].equals("Memberhip Expiration:")||(jTable.getRowCount()==0))
					memberShip.setEnabled(false);
		}
		return memberShip;
	}

	/**
	 * This method initializes buyMemberShip	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBuyMemberShip() {
		if (buyMemberShip == null) {
			buyMemberShip = new JButton();
			buyMemberShip.setLayout(new BorderLayout());
			JLabel label1 = new JLabel("Buy a");
			JLabel label2 = new JLabel("Membership");
			buyMemberShip.add(BorderLayout.NORTH,label1);
			buyMemberShip.add(BorderLayout.SOUTH,label2);
			buyMemberShip.setBounds(new Rectangle(333, 305, 126, 52));
			buyMemberShip.addActionListener(this);
		}
		return buyMemberShip;
	}

	/**
	 * This method initializes cancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCancel() {
		if (cancel == null) {
			cancel = new JButton("Back");
			cancel.setBounds(new Rectangle(404, 25, 83, 29));
			cancel.addActionListener(this);
		}
		return cancel;
	}

	/**
	 * this method is called if one of the buttons is pressed, it calls a method in the control
	 */
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource()==getCancel()) Main.cart.g_exit();
		if(ae.getSource()==getBuyMemberShip()) Main.cart.g_buywithCrditCard(false);
		if(ae.getSource()==getCreditCard()) Main.cart.g_buywithCrditCard(true);
		if(ae.getSource()==getMemberShip()) Main.cart.g_buyWithMembership();
		if(ae.getSource()==getRemove()) Main.cart.g_remove();
		
	}

	/**
	 * This method initializes remove	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getRemove() {
		if (remove == null) {
			remove = new JButton("remove");
			remove.setBounds(new Rectangle(390, 204, 83, 29));
			remove.addActionListener(this);
			if(jTable.getSelectedRow()==-1)
				remove.setEnabled(false);
		}
		return remove;
	}
	
	
	/** implemented method - unused */
	public void valueChanged(ListSelectionEvent e){	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			model=new DefaultTableModel(data,ColumnNames);
			jTable = new JTable(model);
			jTable.addFocusListener(new java.awt.event.FocusListener() {
				public void focusGained(java.awt.event.FocusEvent e) 
				{
					remove.setEnabled(true);
				}
				public void focusLost(java.awt.event.FocusEvent e) 
				{
				}
			});
			TableColumn column=jTable.getColumnModel().getColumn(0);
			column.setPreferredWidth(300);
			column=jTable.getColumnModel().getColumn(1);
			column.setPreferredWidth(150);
			column=jTable.getColumnModel().getColumn(2);
			column.setPreferredWidth(100);
			column=jTable.getColumnModel().getColumn(3);
			column.setPreferredWidth(70);
			
			
		}
		return jTable;
	}

	/**
	 * This method initializes exit and adds listener	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getExit() {
		if (exit == null) {
			exit = new JButton("Logout");
			exit.setBounds(new Rectangle(295, 26, 85, 29));
			exit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Main.cart.exit();
				}
			});
		}
		return exit;
	}

}
